﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

/// <summary>
/// PictureProcessing 的摘要说明
/// </summary>
public class PictureProcessing
{
    public PictureProcessing()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 上传左侧显示图片、基本信息图片
    /// </summary>
    /// <param name="oFile"></param>
    /// <returns></returns>
    //public static string SaveFilePic(HttpPostedFile oFile )
    //{
    //    if (oFile == null || oFile.ContentLength == 0)
    //    {
    //        return "";
    //    }
    //    try
    //    {
            
    //        string strDate = GetCurrentDate().Replace("-", "").Replace("/", "");
    //        string strPath = System.Web.HttpContext.Current.Server.MapPath(@"~\uploadImg\productImage\");
    //        string strDirPath = System.Web.HttpContext.Current.Server.MapPath(@"~\uploadImg\productImage\") + "\\" + strDate;
    //        if (!System.IO.Directory.Exists(strDirPath))
    //        {
    //            System.IO.Directory.CreateDirectory(strDirPath);
    //        }

    //        string sFilePath = strPath + strDate + "\\" + oFile.FileName;
    //        oFile.SaveAs(sFilePath);

    //        return "~\\uploadImg\\productImage\\" + strDate + "\\" + oFile.FileName;
    //    }
    //    catch (System.Exception e)
    //    {
    //        return "";
    //    }
    //}
    /// <summary>
    /// 保存首页的轮流显示的图片信息
    /// </summary>
    /// <param name="oFile"></param>
    /// <returns></returns>
    public static string SaveIndexPics(HttpPostedFile oFile,string fileName)
    {
        if (oFile == null || oFile.ContentLength == 0)
        {
            return "";
        }
        try
        {
            string sFileExt = System.IO.Path.GetExtension(oFile.FileName).ToLower();
            if (sFileExt != ".jpg" && sFileExt != ".jpeg" && sFileExt != ".gif" && sFileExt != ".bmp")
            {
                return "";
            }
            string strPath = System.Web.HttpContext.Current.Server.MapPath(@"~\uploadImg\indexImage\");
            string strDirPath = System.Web.HttpContext.Current.Server.MapPath(@"~\uploadImg\indexImage\")  ;
            if (!System.IO.Directory.Exists(strDirPath))
            {
                System.IO.Directory.CreateDirectory(strDirPath);
            }

            
            string sFilePath = strPath + "\\" + fileName + sFileExt;
            oFile.SaveAs(sFilePath);

            return "~\\uploadImg\\indexImage\\" + fileName + sFileExt;
  
        }
        catch (System.Exception e)
        {
            return "";
        }
    }
    /// <summary>
    /// 存放缩略图
    /// </summary>
    /// <param name="oFile"></param>
    /// <returns></returns>
    public static bool SaveIndexFileThumPic(string strSrcPath, string strPath)
    {
        if (strSrcPath == null || strSrcPath == "")
        {
            return false;
        }
        try
        {
            strSrcPath = System.Web.HttpContext.Current.Server.MapPath(strSrcPath);
            MakeThum(strSrcPath, strPath, 50, 70, "H");   //生成缩略图的方法
            return true;
        }
        catch (System.Exception e)
        {
            return false;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="oFile"></param>
    /// <param name="FileName">不带扩展名的文件名</param>
    /// <returns></returns>
    public static string SaveFilePic(HttpPostedFile oFile,string FileName)
    {
        if (oFile == null || oFile.ContentLength == 0)
        {
            return "";
        }
        try
        {

            string strDate = GetCurrentDate().Replace("-", "").Replace("/", "");
            string strPath = System.Web.HttpContext.Current.Server.MapPath(@"~\uploadImg\productImage\");
            string strDirPath = System.Web.HttpContext.Current.Server.MapPath(@"~\uploadImg\productImage\") + "\\" + strDate;
            if (!System.IO.Directory.Exists(strDirPath))
            {
                System.IO.Directory.CreateDirectory(strDirPath);
            }
            string sFileExt = System.IO.Path.GetExtension(oFile.FileName).ToLower();
            string sFilePath = strPath + strDate + "\\" + FileName + sFileExt;
            oFile.SaveAs(sFilePath);

            return "~\\uploadImg\\productImage\\" + strDate + "\\" + FileName + sFileExt;
        }
        catch (System.Exception e)
        {
            return "";
        }
    }
 

    /// <summary>
    /// 存放商品缩略图
    /// </summary>
    /// <param name="oFile"></param>
    /// <returns></returns>
    public static bool SaveProductFileThumPic(string strSrcPath,string strPath) {
        if (strSrcPath == null || strSrcPath == "")
        {
            return false;
        }
        try
        {
            strSrcPath = System.Web.HttpContext.Current.Server.MapPath(strSrcPath);
            MakeThum(strSrcPath, strPath, 120, 160, "HW");   //生成缩略图的方法
            return true;
        }
        catch (System.Exception e)
        {
            return false;
        }
    }

    /// <summary>
    /// 生成缩略图
    /// </summary>
    /// <param name="originalImagePath">原图路径(物理路径)</param>
    /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
    /// <param name="width">缩略图宽度</param>
    /// <param name="height">缩略图高度</param>
    /// <param name="mode">生成缩略图的方式</param>
    public static void MakeThum(string originalImagePath, string thumbnailPath, int width, int height, string mode)
    {
        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
        int towidth = width;
        int toheight = height;

        int x = 0;
        int y = 0;
        int ow = originalImage.Width;
        int oh = originalImage.Height;

        switch (mode)
        {
            case "HW"://指定高宽缩放(可能变形)
                break;
            case "W"://指定宽，高按比例
                toheight = originalImage.Height * width / originalImage.Width;
                break;
            case "H"://指定高，宽按比例
                towidth = originalImage.Width * height / originalImage.Height;
                break;
            case "Cut"://指定高宽裁剪(不变形)
                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                {
                    oh = originalImage.Height;
                    ow = originalImage.Height * towidth / toheight;
                    y = 0;
                    x = (originalImage.Width - ow) / 2;
                }
                else
                {
                    ow = originalImage.Width;
                    oh = originalImage.Width * height / towidth;
                    x = 0;
                    y = (originalImage.Height - oh) / 2;
                }
                break;
            default:
                break;
        }
        //新建一个bmp图片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
        //新建一个画板
        Graphics g = System.Drawing.Graphics.FromImage(bitmap);
        //设置高度量插值法
        g.InterpolationMode = InterpolationMode.High;
        //设置高质量，低速度呈现平滑度
        g.SmoothingMode = SmoothingMode.HighQuality;
        //清空画布并以透明背景色填充
        g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);

        try
        {
            //以jpg格式保存缩略图
            bitmap.Save(thumbnailPath, ImageFormat.Jpeg);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            originalImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }
    }

    //得到当前日期
    public static string GetCurrentDate()
    {
        return System.DateTime.Now.ToShortDateString();
    }

    public static string GetThumbPath(string strSrcPath)
    {
        string strFullPath = System.Web.HttpContext.Current.Server.MapPath(strSrcPath);
        string strDirectory = System.IO.Path.GetDirectoryName(strFullPath);
        string strFileName = System.IO.Path.GetFileNameWithoutExtension(strFullPath);
        string strFileExt = System.IO.Path.GetExtension(strFullPath);

        string strThumbPath = strDirectory + "\\" + strFileName + "_thumb" + strFileExt;
        return strThumbPath;
    }
    public static string GetThumbURL(string strSrcURL)
    {
        int idx = strSrcURL.LastIndexOf('.');
        string strDir = strSrcURL.Substring(0, idx);
        string strExt = strSrcURL.Substring(idx, strSrcURL.Length - strDir.Length);
        string strURL = strDir + "_thumb" + strExt;
        string strUpURL = strURL.Replace(@"\", "/");
        return strUpURL;
    }

    /// <summary>
    /// 地址"~/img/goods/***.jpg" 转化 "./img/goods/***_thumb.jpg"
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string ImgPathToThumbURL(string path)
    {
        string strThumbPath = GetThumbURL(path);
        return strThumbPath.Replace('~', '.');
    }
}
